import requests



# 登录 -- 返回令牌
resp = requests.post(url='http://ihrm-test.itheima.net/api/sys/login',
                     headers={'Content-Type': 'application/json'},
                     json={'mobile': '13800000002', 'password': '123456'})
print(resp.json())

# 添加员工
# 获取令牌
token = 'Bearer ' + resp.json().get('data')

# 组织添加员工的数据信息 --- 测试时，保证手机号唯一
json_data = {"username": "尼古拉斯刘能", "mobile": "13988799872",
             "timeOfEntry": "2021-04-15", "formOfEmployment": 1,
             "workNumber": "9527", "departmentName": "测试部",
             "departmentId": "1063678149528784896",
             "correctionTime": "2021-04-29T16:00:00.000Z"}

resp = requests.post(url='http://ihrm-test.itheima.net/api/sys/user',
                     headers={'Content-Type': 'application/json',
                              'Authorization': token},
                     json=json_data)
print(resp.json())

# 获取添加员工成功的 id
emp_id = resp.json().get('data').get('id')
print('获取的员工id为：', emp_id)

# 查询员工 --- 依赖 员工id。 路径参数中的 :target 应该使用 emp_id 替换
resp = requests.get(url='http://ihrm-test.itheima.net/api/sys/user/' + emp_id,
                    headers={'Content-Type': 'application/json',
                             'Authorization': token})
print(resp.json())

# 修改员工
resp = requests.put(url='http://ihrm-test.itheima.net/api/sys/user/' + emp_id,
                    headers={'Content-Type': 'application/json',
                             'Authorization': token},
                    json={'username': '大侠马保国'})
print(resp.json())

resp = requests.put(url='http://ihrm-test.itheima.net/api/sys/user/'+emp_id,
                    headers={'Content-Type': 'application/json',
                             'Authorization': token},
                    json={'username':'哈哈'})

print(resp.json())

resp = requests.delete(url='http://ihrm-test.itheima.net/api/sys/user/'+emp_id,
                    headers={'Content-Type': 'application/json',
                             'Authorization': token})
print(resp.json())

resp = requests.get(url='http://ihrm-test.itheima.net/api/sys/user?page=1&size=10',
                    headers={'Content-Type': 'application/json',
                             'Authorization': token})
print('查询员工列表',resp.json())





